/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package it.master.cloud.carsharing.dbmanager;

import java.sql.*;

/**
 *
 * @author cloud
 */
public class DBManager {
 
    public static Connection accessDB() {
        String url = "jdbc:derby://localhost:1527/carSharing";
        String user = "app";
        String pwd = "app";
        try {		  // registrazione driver JDBC per Derby DB
            DriverManager.registerDriver(new org.apache.derby.jdbc.ClientDriver());
            // richiedo apertura di connessione al DB
            Connection c = DriverManager.getConnection(url, user, pwd);
           
            return c;
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        
        return null;
    }

    public static String controlLogin(String login) {
        
        String result = "";
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // richiedo apertura di connessione al DB
            con = DBManager.accessDB();
            
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT user_id FROM users WHERE login='" + login + "'");
            // displaying records
            while (rs.next()) {
                result = "Utente non disponibile! Scegline un altro";
            }
            rs.close();
            stmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return result;

    }
    
    public static String controlLogin(String login, String password) {
        
        String result = "";
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // richiedo apertura di connessione al DB
            con = DBManager.accessDB();
            
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT user_id FROM users WHERE login='" + login + "' AND password='" + password + "'");
            // displaying records
            result = "ko";
            while (rs.next()) {
                result = "success";
            }
            rs.close();
            stmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return result;

    }
    
    public static String getUserId(String login, String password) {
        
        String result = "";
        String userId = "";
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // richiedo apertura di connessione al DB
            con = DBManager.accessDB();
            
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT user_id FROM users WHERE login='" + login + "' AND password='" + password + "'");
            // displaying records
            result = "ko";
            
            while (rs.next()) {
                userId = rs.getString("user_id");
            }
            rs.close();
            stmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return userId;
    }
}
